<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\model\ClassifyModel;
use app\common\model\GoodsModel;

class Classify extends Base
{
    // 类别列表
    public function index()
    {
        if(request()->isAjax()){

            $classify = new ClassifyModel();
            $classifys = $classify->getClassifyList();

            foreach ($classifys as $k=>$v) {
                $classifys["$k"]['name'] = $v['name'];
//                $classifys["$k"]['clas_explain'] = $txt = @str_replace("\n", "\n".'       ', $v['clas_explain']);
            }

            $classifys = getTree(objToArray($classifys), false);
            return json(msg(1, $classifys, 'ok'));
        }

        return $this->fetch();
    }

    // 添加类别
    public function classifyAdd()
    {
        $param = input('post.');

        $classify = new ClassifyModel();
        $flag = $classify->insertClassify($param);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }

    // 编辑类别
    public function classifyEdit()
    {
        $param = input('post.');

        $classify = new ClassifyModel();
        $flag = $classify->editClassify($param);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }

    // 删除类别
    public function classifyDel()
    {
        $id = input('param.id');
        $classify = new ClassifyModel();
        //判断商品有没有引用

        $goods = new GoodsModel();

        $where['good_classify'] = $id;
        $where['good_delete'] = 0;
        $rows = $goods->getAllGoods($where);

        if ($rows > 0){
            return json(msg(-1, '', '有商品正在使用 不能删除'));
        }

        $flag = $classify->delClassify($id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }
}
